package com.foreveredu.propms.web.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.ParkLocation;
import com.foreveredu.propms.service.IChargeService;
import com.foreveredu.propms.web.struts.actionform.ParkLocationActionForm;

public class ParkLocationAction extends AbstractBaseDispatchAction {

	private IChargeService chargeService = null;

	public IChargeService getChargeService() {
		return chargeService;
	}

	public void setChargeService(IChargeService chargeService) {
		this.chargeService = chargeService;
	}

	// 跳转到收费项目列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<ParkLocation> parkLocationList = chargeService.findParkLocations();
		request.setAttribute("parkLocationList", parkLocationList);

		return mapping.findForward("parkLocationList");
	}

	// 完成收费项目添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ParkLocationActionForm plf = (ParkLocationActionForm) form;
		ParkLocation ParkLocation = new ParkLocation();
		PropertyUtils.copyProperties(ParkLocation, plf);
		if (plf.getName() == null || "".equals(plf.getName())) {
			request.setAttribute("parkLocation", ParkLocation);
			request.setAttribute("error", "停车位置不能为空！");
			return mapping.findForward("toParkLocationList");
		}
		chargeService.saveParkLocation(ParkLocation);
		return mapping.findForward("toParkLocationList");
	}

	// 跳转到收费项目修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ParkLocationActionForm plf = (ParkLocationActionForm) form;
		ParkLocation ParkLocation = chargeService.findParkLocationById(plf.getId());
		request.setAttribute("parkLocation", ParkLocation);

		return mapping.findForward("ParkLocationUpdate");
	}

	// 保存收费项目修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ParkLocationActionForm plf = (ParkLocationActionForm) form;
		ParkLocation ParkLocation = chargeService.findParkLocationById(plf.getId());
		ParkLocation.setName(plf.getName());
		if(plf.getName()==null || "".equals(plf.getName()) ){
			request.setAttribute("parkLocation", ParkLocation);
			request.setAttribute("error","停车位置不能为空！");
			return mapping.findForward("ParkLocationUpdate");
		}
		chargeService.updateParkLocation(ParkLocation);
		return mapping.findForward("update");
	}

	// 完成收费项目删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ParkLocationActionForm plf = (ParkLocationActionForm) form;
		chargeService.deleteParkLocationById(plf.getId());
		return mapping.findForward("delete");
	}

}
